package com.example.ntweixin.utils;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;


public class HttpUtil {

    private static Logger logger = LoggerFactory.getLogger(HttpUtil.class);

    public static void main(String[] args) {

    }

    /**
     * http post 请求
     *
     * @param uri
     * @param body
     * @return
     */
    public static String httpPost(String uri, String body) {
        logger.info("uri ---->>" + uri);
        logger.info("body ---->> \n" + body + "\n");
        String responseBody = "";
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(uri);
        post.addRequestHeader("text/plain", "UTF-8");

        try {
            StringRequestEntity entity = new StringRequestEntity(body, null, null);
            post.setRequestEntity(entity);

            // Execute the method.
            int statusCode = client.executeMethod(post);

            logger.info("status code --->>" + statusCode);
            if (statusCode != HttpStatus.SC_OK) {
                logger.error("Method failed: " + post.getStatusLine());
            }

            // Read the response body.
            byte[] responseBodyB = post.getResponseBody();

            // Deal with the response.
            // Use caution: ensure correct character encoding and is not binary data
            responseBody = new String(responseBodyB);
            logger.info("responseBody ---->>" + responseBody);

        } catch (HttpException e) {
            logger.error(e.getMessage(), e);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        } finally {
            // Release the connection.
            post.releaseConnection();
        }
        return responseBody;
    }

    public static String httpGet(String uri) {
        String body = "";
        HttpClient client = new HttpClient();
        HttpMethod method = new GetMethod(uri);
        try {
            // Execute the method.
            int statusCode = client.executeMethod(method);

            if (statusCode != HttpStatus.SC_OK) {
                logger.error("Method failed: " + method.getStatusLine());
            }

            // Read the response body.
            byte[] responseBody = method.getResponseBody();

            // Deal with the response.
            // Use caution: ensure correct character encoding and is not binary data
            logger.info(new String(responseBody));

        } catch (HttpException e) {
            logger.error(e.getMessage(), e);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        } finally {
            // Release the connection.
            method.releaseConnection();
        }
        return body;
    }

    /**
     * 读取微信素图片资源
     */
    public static void httpPostForMedia(String url, String body, String imageFilePathname, boolean isScalr) {

        // TODO
    }
}
